package com.dd.util;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import java.sql.*;
import javax.mail.Header;
import javax.resource.cci.Record;

import java.util.Enumeration;

public class MailTest {
    public static void main(String arg[]){
        new MailTest().test2();

    }
        public void test() {
                try {
//                    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
//                                                .newInstance();
//                                String url = "jdbc:microsoft:sqlserver://192.168.1.96:1433;DatabaseName=mail";
//                                Connection conn = DriverManager.getConnection(url, "sa", "sa");
//                                Statement stmt = conn.createStatement();
//                                //10年的发货单,根据子表里面合同新业务员,直接更新发货单业务员字段
//                                String sql0 = "select mailcontent from vn_mailcontent where id=2 ";
//                                String str="";
//                                ResultSet rs0 = stmt.executeQuery(sql0);
//                                if(rs0.next()){
//                                        str=rs0.getString("mailcontent");
//                                }
                                /*
                        String host = "smtp.263.net";
                        String from = "pickgoods@263.net";
                        */
                    String host = "smtp.163.com";
                    String from = "smart_886@163.com";
                        String to = "124889695aa@qq.com";
                        /*
                        final String username = "pickgoods"; // 发件人的邮件帐户
                        final String password = "wjgxls.."; // 发件人的邮件密码
                        */
                       final String username = "smart_886"; // 发件人的邮件帐户
                       final String password = "zzzz83156";
                        // Get system properties
                        Properties props = System.getProperties();
                        // Setup mail server
                        props.put("mail.smtp.host", host);
                        props.put("mail.smtp.auth", "true");
                        MyAuthenticator ma = new MyAuthenticator();
                        ma.setPassword(password);
                        ma.setUsername(username);
                        Session session = Session.getInstance(props, ma);


                        MimeMessage message = new MimeMessage(session);
                       // message.set
                       // message.setSender(new InternetAddress("alldaybuy@263.net","denes"));
                      //  message.addRecipient(type, new InternetAddress("smart_886@163.com","linjie"));
                       // InternetAddress re = new InternetAddress("smart_886@163.com","linjie");
                        //InternetAddress re1 = new InternetAddress("smart_885@163.com","linji555");
                       // InternetAddress rer[] ={re,re1};
                       // message.setReplyTo(rer);
                       message.setSentDate(new Date());

                        message.setFrom(new InternetAddress(from,"denes"));
                        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
                       // message.addRecipient(Message.RecipientType.BCC, new InternetAddress("enjoyfoot@hotmail.com"));
                       // message.addRecipient(Message.RecipientType.BCC, new InternetAddress("r0711@hotmail.com"));
                        //message.addRecipient(Message.RecipientType.BCC, new InternetAddress("wwwsneakerokcom@hotmail.com"));
                       // message.setSubject("Test000001");
                        message.getAllHeaders();


                        //str.set
                        message.setContent("你好","text/html;charset=utf-8");
                        
                        message.setHeader("X-MSMail-Priority","Normal");
                        message.setHeader("X-Priority","3");
                        message.setHeader("MIME-Version","1.0");
                        message.setHeader("X-Mailer","Microsoft Outlook Express 6.00.2900.5843");
                        message.setHeader("X-MimeOLE","Produced By Microsoft MimeOLE V6.00.2900.5579");
                        message.setHeader("Reply-To","ajsneaker@263.net");
                      //  message.setText("TEST BCC");
                         //Send message
                         Enumeration headerParts = message.getAllHeaders();

                       Header header;

                       while (headerParts.hasMoreElements()) {
                           header = (Header) headerParts.nextElement();
                           System.out.println(header.getName()+" : " + header.getValue());
                       }
                       /**
                       1 From: "smart" <smart_886@163.com>
                       1 To: <124889695@qq.com>
                       1 Subject: tst
                       1 Date: Fri, 21 May 2010 10:09:25 +0800
                       1 MIME-Version: 1.0
                        Content-Type: multipart/alternative;
                        boundary="----=_NextPart_000_0003_01CAF8CD.B1384700"
                       1 X-Priority: 3
                       1 X-MSMail-Priority: Normal
                        X-Mailer: Microsoft Outlook Express 6.00.2900.5843
                        X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
==================================================================================================
                        Date : Fri, 21 May 2010 10:15:35 +0800 (CST)
                        From : denes <smart_886@163.com>
                        To : wwwsneakerokcom@yahoo.com
                        Bcc : enjoyfoot@hotmail.com
                        Bcc : r0711@hotmail.com
                        Bcc : wwwsneakerokcom@hotmail.com
                        Subject : Test000001


                        */
                       Transport.send(message);
                } catch (Exception e) {
                        e.printStackTrace();
                }

        }
        public void test2(){
        	try{
        	String mail = "124889695@qq.com";
        	Socket socket = new Socket("smtp.263.net", 25);
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            in.readLine();
            out.write("HELO Sender ");
            out.flush();
            in.readLine();
            out.write("MAIL FROM:<56310296@263.net> ");
            out.flush();
            in.readLine();
            out.write("RCPT TO:<56310296@263.net> ");
            out.flush();
            String r = in.readLine();
            out.write("QUIT ");
            out.flush();
            System.out.println(r);
            out.close();
            in.close();
            socket.close();
        	}catch(Exception e){
        		e.printStackTrace();
        	}

        	
        }


}
